//
//  ViewController.swift
//  XCDragCardContainer
//
//  Created by guoshuai.cheng@holla.world on 05/29/2023.
//  Copyright (c) 2023 guoshuai.cheng@holla.world. All rights reserved.
//

import UIKit
import XCDragCardContainer
import SnapKit

private var titles: [String] = ["水星1", "金星2", "地球3", "火星4", "木星5", "土星6"]
private let allowedDirection: [Direction] = [.left,.right]

class ViewController: UIViewController {

    private lazy var cardContainer: DragCardContainer = {
        let cardContainer = DragCardContainer()
        // 是否可以无限滑动
        cardContainer.infiniteLoop = true
        // 数据源
        cardContainer.dataSource = self
        // 代理
//        cardContainer.delegate = self
        // 可见卡片数量
        cardContainer.visibleCount = 1
        // 是否禁用卡片拖动
        cardContainer.disableTopCardDrag = false
        // 是否禁用卡片点击
        cardContainer.disableTopCardClick = false
        
        let mode = ScaleMode()
        // 卡片之间间距
        mode.cardSpacing = 10
        // 方向（可以运行Demo，修改该参数看实际效果）
        mode.direction = .bottom
        // 最小缩放比例
        mode.minimumScale = 0.7
        // 卡片最大旋转角度
        mode.maximumAngle = 0
        // 赋值mode
        cardContainer.mode = mode
        
        return cardContainer
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(cardContainer)
        
        cardContainer.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.leading.equalTo(15)
            make.top.equalToSuperview().offset(163)
            make.bottom.equalToSuperview().offset(-107)
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController: DragCardDataSource {
    public func numberOfCards(_ dragCard: DragCardContainer) -> Int {
        print("XXXXXXXX \(titles.count)")
        return titles.count
    }

    public func dragCard(_ dragCard: DragCardContainer, viewForCard index: Int) -> DragCardView {
        if titles.count == 0 {
            return DragCardView()
        }
        
        let cardView = OnlineUserView.show()
        print("加载第几个？？？: \(index)")
        cardView.allowedDirection = allowedDirection
        cardView.userNameLabel.text = "\(titles[index])"
        cardView.chatActionBlock = { [weak self] in
            guard let `self` = self else { return }
            self.openConversation(index: index)
        }
        return cardView
    }
    
    func openConversation(index: Int) {
        titles.remove(at: index)
        cardContainer.reloadData()
        cardContainer.removeTopCard(to: .right)
    }
}

//extension ViewController: DragCardDelegate {
//    public func dragCard(_ dragCard: DragCardContainer, didSelectTopCardAt index: Int, with cardView: DragCardView) {
//        print("删除第几个？？: \(index)")
////        titles.remove(at: index)
////        cardContainer.swipeTopCard(to: .right)
////        cardContainer.reloadData(forceReset: false,animation: true)
//    }
//}


 
